//
//  main.cpp
//  类成员访问运算符
//
//  Created by linjinzhu on 2017/7/12.
//  Copyright © 2017年 linjinzhu. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;

class Obj {
    static int i,j;
public:
    void f() {cout << ++i << endl;};
    void g() {cout << ++j << endl;};

};

int Obj::i = 10;
int Obj::j = 20;

class ObjContainer {
    vector<Obj*> a;

public:
    void add(Obj *obj) {
        a.push_back(obj);
    }

    friend class SmartPointer;
};

class SmartPointer {
    ObjContainer oc;
    int index;
public:
    SmartPointer(ObjContainer &objc) {
        oc = objc;
        index = 0;
    }

    bool operator++() {
        if (index >= oc.a.size()) {
            return false;
        }

        if (oc.a[++index] == 0) {
            return false;
        }

        return true;
    }

    bool operator++(int) {
        return operator++();
    }

    Obj* operator->() const {
        if (!oc.a[index]) {
            cout << "Zero Value" << endl;
            return (Obj*)0;
        }

        return oc.a[index];
    }
};

int main(int argc, const char * argv[]) {

    const int size = 10;
    Obj obj[size];
    ObjContainer oc;

    for (int i=0; i<size; i++) {
        oc.add(&obj[i]);
    }

    SmartPointer sp(oc);
    do {
        sp->g();
        sp->f();
    } while (++sp);


    return 0;
}
